<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
/**
 * 洗涤小助手-卖场活动 控制器
 * @author 咩咩 <liuyangvsms@163.com>
 */
class ActivityController extends AdminController {

    /**
     * 卖场活动-总览
     */
    public function index(){
        /*--------------------------测试开始--------------------------*/
//        $name = '名字';
//        error_log('name='.$name."\n", 3, "test.log");//打印日志

//        $address = '广东省广州市黄埔区石化路123号';
//        //调用百度地图接口
//        $map = new \Vendor\address\Baidu_map();
//        $res = $map->getLngLat($address);
//        print_r($res);


//        /* 生成html文件 */
//        $savename = time().'_'.mt_rand(1000000,9999999); //文件名
//        $html_path = 'Uploads/Activity/lunbo/html/'; //html文件目录
//
//        $TxtFileName = $html_path.$savename.".html";
//        //以读写方式打写指定文件，如果文件不存则创建
//        $TxtOpen = fopen($TxtFileName,"w");
//        if($TxtOpen === FALSE){
//            $this->error("创建可写文件：".$TxtFileName."失败!");
//        }
//
//        $html_top = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body>';
//        $html_bottom = '</body></html>';
//        $html_content = $html_top.'<h2 align="center"><span>测试</span></h2>'.$html_bottom;
//
//        //写入文件
//        $TxtWrite = fwrite($TxtOpen,$html_content);
//        if(!$TxtWrite){
//            fclose($TxtOpen);
//            $this->error("尝试向文件".$TxtFileName."写入".$html_content."失败！");
//        }
//        fclose ($TxtOpen); //关闭指针

//        unlink('../wash_helper/test.log');


//        /*正则表达式提取图片路径*/
//        $str = '<p>
//                    测试
//                </p>
//                <p>
//                    <a class="ke-insertfile" href="/wash_helper/Uploads/Editor/2018-09-28/5bae12c309ff1.jpg" target="_blank">/wash_helper/Uploads/Editor/2018-09-28/5bae12c309ff1.jpg</a>
//                </p>
//                <p>
//                    <img src="/wash_helper/Uploads/Editor/2018-09-28/5bae12cdeeb75.jpg" alt="" width="182" height="162" />
//                </p>
//                <p>
//                    <img src="/wash_helper/Uploads/Editor/2018-09-28/5bae12asdsd54.mp4" alt="" width="182" height="162" />
//                </p>
//                <p>
//                    <a class="ke-insertfile" href="/wash_helper/Uploads/Editor/2018-09-28/5bae12d8bbf82.mp4" target="_blank">文件</a>
//                </p>';
//        preg_match_all('/(src|href)=\"?(.+\.(jpg|gif|jpeg|png|mp4|webm|ogg))\"/i',$str,$match);
//        print_r($match);
//        if($match[1]){
//            echo 111;
//        }else{
//            echo 222;
//        }

//        $str = '   广东省深圳市  南山区  后海大道3012   ';
//        $str = preg_replace('/(\s|\&nbsp\;|　|\xc2\xa0)/','',$str);
//        print_r($str);

//        $str = '201803220534215432105';
//        $s = (int)substr($str,-2);
//        print_r($s);

//        $cid = date('YmdHis').mt_rand(10000, 99999).wash_table_id('wash_activity_store');
//        print_r($cid);

//        $table_id = wash_table_id('wash_activity_store');
//        print_r($table_id);

//        $table = wash_table(1);
//        print_r($table);

//        print_r(create_cid('wash_activity_store'));

//        $c_list = array("111","222","333");
//        $cid = (string)222;
//        $c_key = array_search($cid,$c_list);
//        print_r($c_key);
//        unset($c_list[$c_key]);
//        print_r($c_list);
//        sort($c_list);
//        print_r($c_list);


//        // 起点坐标
//        $longitude1 = 113.856236;
//        $latitude1 = 22.598742;
//
//        // 终点坐标
//        $longitude2 = 113.855884;
//        $latitude2 = 22.60285;
//
//        //调用地图距离计算接口
//        $map_other = new \Vendor\address\Map_other();
//        $distance = $map_other->getDistance($longitude1, $latitude1, $longitude2, $latitude2, 2);
//        echo $distance;




        /*$data = array(
            "openId"=>"oqT_E5CkzGRu3rvug2DBavDVbgDF",
            "nickName"=>"Elva",
	        "gender"=>2,
            "language"=>"zh_CN",
            "city"=>"Shenzhen",
            "province"=>"Guangdong",
            "country"=>"China",
            "avatarUrl"=>"https://wx.qlogo.cn/mmopen/vi_32/QQicqq0Cy6pKVnBiaNXZYLSFzNoc6GXZ5KLYCwHITHoTwCrqHCvjlO4nCpBJBeA9fbTN3SibVLmHexUS05U5upX2w/132",
            "unionId"=>"opbF5iJrnR4NFnW1TnZUHpMfFeYa"
        );

        $db_wash_user = M('wash_user');//实例化用户信息表

        //返回值数组
        $de_res = array('msg'=>'', 'error'=> 0, 'data'=> array());

        $unionId = $data['unionId'];
        $wash_user = $db_wash_user->where("unionId='$unionId'")->field('uid')->find();
        if ($wash_user) {
            $de_res['data']['uid'] = $wash_user['uid'];
        } else {
            $uid = date('YmdHis').mt_rand(10000, 99999);
            $info['uid'] = $uid;
            $info['openId'] = $data['openId'];
            $info['unionId'] = $data['unionId'];
            $add_wash_user = $db_wash_user->data($info)->add();
            if ($add_wash_user) {
                $de_res['data']['uid'] = $uid;
            }else{
                $de_res['msg'] = '用户信息添加失败';
                $de_res['error'] = 402;
                $en_res = json_encode($de_res, 320);
                exit($en_res);
            }
        }

        //转成json形式
        //JSON_UNESCAPED_UNICODE = 256
        //JSON_UNESCAPED_SLASHES = 64
        $en_res = json_encode($de_res, 320);

        echo $en_res;*/


//        $type = 03;
//        print_r(wash_table($type));



        /*$list = array(
            array(
                array(
                    'id'=>'11',
                    'title'=>'liuyang'
                ),
                array(
                    'id'=>'12',
                    'title'=>'luoying'
                ),
                array(
                    'id'=>'13',
                    'title'=>'lianshengjie'
                )
            ),
            array(
                array(
                    'id'=>'21',
                    'title'=>'xiaohua'
                ),
                array(
                    'id'=>'22',
                    'title'=>'xiaoming'
                )
            )
        );
        $this->assign('list', $list); //赋值数据集*/



//        $image = $_FILES["photo"]["tmp_name"];
//        $fp = fopen($image, "r");
//        $file = fread($fp, $_FILES["photo"]["size"]); //二进制数据流
//        error_log($file."\n", 3, "test.log");//打印日志
//        $jpg = $file;

        //生成图片
//        $jpg = file_get_contents('php://input', 'r');//得到post过来的二进制原始数据
//        $jpg = substr($jpg, 4);
//        error_log($jpg."\n", 3, "test.log");//打印日志

//        $filename="QR".".jpg";///要生成的图片名字
//        $file = fopen("./".$filename,"w");//打开文件准备写入
//        fwrite($file,$jpg);//写入
//        fclose($file);//关闭

//        $ret = file_put_contents('./test.jpg', $jpg, true);
//        error_log('$ret='.$ret."\n", 3, "test.log");//打印日志


//        $db_wash_calculation_function = M('wash_calculation_function');//实例化洗涤功能表
//        $wash_not_first = $db_wash_calculation_function->where("belong_id=0")->field('p_list')->select();
//        echo $db_wash_calculation_function->getDbError();
//        $p_list = json_decode($wash_not_first[0]['p_list'], true);
//        print_r($p_list);
//        print_r($p_list[1][array_rand($p_list[1])]);

//        error_log('$_POST='.json_encode($_POST, 320)."\n", 3, "test.log");//打印日志

//        $url = "http://bmcrm.bluemoon.com.cn/bmcrm-control/custConfirm/getQRCodeStatus?client=android&cuid=024b86b5-45cc-4d36-a50c-494d764c7802&version=2.4.1&format=json&time=1540363398469&source=sfa&sign=ccd9aea51ed5182484728f9faeb5c393";
//        $url = "https://tmallapi.bluemoon.com.cn/bmcrm-control/custConfirm/getQRCodeStatus?client=ios&cuid=78771ECF-8D45-4E16-82AE-DDF0A9C4280F&format=json&sign=455e2b1c29cc7210df943acaa454630e&time=1459999388&version=1.0.0&dataSource=null";
//        $data = array('id'=>'https://app1.bluemoon.com.cn/sfa?id=Q295B0DE65ACB04o3');
//        $res = $this->sfa_post_curl($url,$data);
//        print_r($res);

        /*--------------------------测试结束--------------------------*/

        $this->meta_title = '卖场活动-总览';//页面title信息
        $this->assign('title', '卖场活动-总览');//页面标题
        $this->display();
    }

    /**
     * 轮播图管理-轮播图
     */
    public function lunbo(){
        $db_wash_activity_lunbo = M('wash_activity_lunbo');//实例化轮播表

        $wash_activity_lunbo = $db_wash_activity_lunbo->order('id DESC')->field('id,img_url,title')->select();
        $arr = $wash_activity_lunbo;

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 10);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出


        $this->meta_title = '轮播图管理-轮播图';//页面title信息
        $this->assign('title', '轮播图管理-轮播图');//页面标题
        $this->display();
    }

    /**
     * 轮播图管理-轮播图-新增
     */
    public function lunbo_add(){
        $db_wash_activity_lunbo = M('wash_activity_lunbo');//实例化轮播表

        if(IS_POST){
            $title = I('post.title');
            $content = I('post.content');

            /*文件上传*/
            $savename = time().'_'.mt_rand(1000000,9999999);
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 2048000;// 设置附件上传大小 2M=2048000
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
            $upload->savePath = './Activity/lunbo/picture/'; // 设置附件上传（子）目录
            $upload->saveName = $savename; // 上传文件的文件名
            //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
            $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
            $upload->replace = true; // true会存在同名文件将覆盖
            // 上传文件
            $upload_info = $upload->upload();
            if (!$upload_info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{
                $img_url = "/wash_helper/Uploads/Activity/lunbo/picture/".$upload_info['file']['savename'];
                $info['img_url'] = $img_url;

                $info['title'] = $title;
                $info['content'] = $content;
                $add_wash_activity_lunbo = $db_wash_activity_lunbo->data($info)->add();
                if($add_wash_activity_lunbo){
                    $this->redirect('Activity/lunbo');
                }else{
                    $this->error('保存失败!');
                }
            }
        }

        $this->meta_title = '轮播图管理-轮播图-新增';//页面title信息
        $this->assign('title', '轮播图管理-轮播图-新增');//页面标题
        $this->display();
    }

    /**
     * 轮播图管理-轮播图-编辑
     */
    public function lunbo_edit(){
        $db_wash_activity_lunbo = M('wash_activity_lunbo');//实例化轮播表

        $id = I('get.id');//轮播图ID
        $lunbo = $db_wash_activity_lunbo->where("id='$id'")->find();
        $this->assign('lunbo', $lunbo);

        if(IS_POST){
            $title = I('post.title');
            $content = I('post.content');

            //判断是否有文件上传
            if($_FILES["file"]["tmp_name"]){
                /*文件上传*/
                $savename = time().'_'.mt_rand(1000000,9999999);
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 2048000;// 设置附件上传大小 2M=2048000
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
                $upload->savePath = './Activity/lunbo/picture/'; // 设置附件上传（子）目录
                $upload->saveName = $savename; // 上传文件的文件名
                //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
                $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
                $upload->replace = true; // true会存在同名文件将覆盖
                // 上传文件
                $upload_info = $upload->upload();
                if (!$upload_info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $img_url = "/wash_helper/Uploads/Activity/lunbo/picture/".$upload_info['file']['savename'];
                    $info['img_url'] = $img_url;

                    /*删除文件*/
                    $img_url_path = '..'.$lunbo['img_url'];
                    if(file_exists($img_url_path)){
                        unlink($img_url_path);
                    }
                }
            }

            $info['title'] = $title;
            $info['content'] = $content;
            $edit_wash_activity_lunbo = $db_wash_activity_lunbo->where("id='$id'")->setField($info);
            if($edit_wash_activity_lunbo){
                $this->redirect('Activity/lunbo');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '轮播图管理-轮播图-编辑';//页面title信息
        $this->assign('title', '轮播图管理-轮播图-编辑');//页面标题
        $this->display();
    }

    /**
     * 轮播图管理-轮播图-删除
     */
    public function lunbo_del(){
        $db_wash_activity_lunbo = M('wash_activity_lunbo');//实例化轮播表

        $id = I('get.id');//轮播图ID
        $lunbo = $db_wash_activity_lunbo->where("id='$id'")->find();

        /*删除文件*/
        $img_url_path = '..'.$lunbo['img_url'];
        if(file_exists($img_url_path)){
            unlink($img_url_path);
        }

        /*删除图文混排图片*/
        $this->del_images_path($lunbo['content'],2);

        $del_wash_activity_lunbo = $db_wash_activity_lunbo->where("id='$id'")->delete();
        if($del_wash_activity_lunbo){
            $this->success('删除成功!');
        }else{
            $this->error('删除失败!');
        }
    }

    /**
     * 轮播图管理-轮播图-详情
     */
    public function lunbo_detail(){
        $db_wash_activity_lunbo = M('wash_activity_lunbo');//实例化轮播表

        $id = I('get.id');//轮播图ID
        $lunbo = $db_wash_activity_lunbo->where("id='$id'")->find();
        $this->assign('lunbo', $lunbo);

        $this->meta_title = '轮播图管理-轮播图-详情';//页面title信息
        $this->assign('title', '轮播图管理-轮播图-详情');//页面标题
        $this->display();
    }

    /**
     * 门店管理-门店信息
     */
    public function store(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        $wash_activity_store = $db_wash_activity_store->order('id DESC')->field('id,store_name,address,brand,province,city,district,banner_img_url')->select();
        $arr = $wash_activity_store;

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 10);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '门店管理-门店信息';//页面title信息
        $this->assign('title', '门店管理-门店信息');//页面标题
        $this->display();
    }

    /**
     * 门店管理-门店信息-新增
     */
    public function store_add(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        if(IS_POST){
            $store_name = I('post.store_name');
            $address = preg_replace('/(\s|\&nbsp\;|　|\xc2\xa0)/','',I('post.address'));
            $brand = I('post.brand');
            $province = I('post.province');
            $city = I('post.city');
            $district = I('post.district');
            $lng = I('post.lng');
            $lat = I('post.lat');
            $map_address = I('post.map_address');

            /*文件上传*/
            $savename = time().'_'.mt_rand(1000000,9999999);
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 2048000;// 设置附件上传大小 2M=2048000
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
            $upload->savePath = './Activity/store/picture/'; // 设置附件上传（子）目录
            $upload->saveName = $savename; // 上传文件的文件名
            //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
            $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
            $upload->replace = true; // true会存在同名文件将覆盖
            // 上传文件
            $upload_info = $upload->upload();
            if (!$upload_info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{
                $banner_img_url = "/wash_helper/Uploads/Activity/store/picture/".$upload_info['file']['savename'];
                $info['banner_img_url'] = $banner_img_url;
                $info['store_name'] = $store_name;
                $info['address'] = $address;
                $info['brand'] = $brand;
                $info['province'] = $province;
                $info['city'] = $city;
                $info['district'] = $district;
                $info['lng'] = $lng;
                $info['lat'] = $lat;
                $info['map_address'] = $map_address;
                $info['c_list'] = '[]';//活动内容列表

                $info['type'] = 3;//类别编号
                $info['datetime'] = date('Y-m-d H:i:s');
                $info['time'] = time();

                /*生成cid*/
                $cid = create_cid('wash_activity_store');
                $info['cid'] = $cid;

                $add_wash_activity_store = $db_wash_activity_store->data($info)->add();
                if($add_wash_activity_store){
                    $this->redirect('Activity/store');
                }else{
                    $this->error('保存失败!');
                }
            }
        }

        $this->meta_title = '门店管理-门店信息-新增';//页面title信息
        $this->assign('title', '门店管理-门店信息-新增');//页面标题
        $this->display();
    }

    /**
     * 门店管理-门店信息-编辑
     */
    public function store_edit(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        $id = I('get.id');//门店信息ID
        $store = $db_wash_activity_store->where("id='$id'")->find();
        $this->assign('store', $store);

        if(IS_POST){
            $store_name = I('post.store_name');
            $address = preg_replace('/(\s|\&nbsp\;|　|\xc2\xa0)/','',I('post.address'));
            $brand = I('post.brand');
            $province = I('post.province');
            $city = I('post.city');
            $district = I('post.district');
            $lng = I('post.lng');
            $lat = I('post.lat');
            $map_address = I('post.map_address');

            //判断是否有文件上传
            if($_FILES["file"]["tmp_name"]){
                /*文件上传*/
                $savename = time().'_'.mt_rand(1000000,9999999);
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 2048000;// 设置附件上传大小 2M=2048000
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
                $upload->savePath = './Activity/store/picture/'; // 设置附件上传（子）目录
                $upload->saveName = $savename; // 上传文件的文件名
                //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
                $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
                $upload->replace = true; // true会存在同名文件将覆盖
                // 上传文件
                $upload_info = $upload->upload();
                if (!$upload_info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $banner_img_url = "/wash_helper/Uploads/Activity/store/picture/".$upload_info['file']['savename'];
                    $info['banner_img_url'] = $banner_img_url;

                    /*删除文件*/
                    $banner_img_url_path = '..'.$store['banner_img_url'];
                    if(file_exists($banner_img_url_path)){
                        unlink($banner_img_url_path);
                    }
                }
            }

            $info['store_name'] = $store_name;
            $info['address'] = $address;
            $info['brand'] = $brand;
            $info['province'] = $province;
            $info['city'] = $city;
            $info['district'] = $district;
            $info['lng'] = $lng;
            $info['lat'] = $lat;
            $info['map_address'] = $map_address;

            /*更新时间*/
            $info['datetime'] = date('Y-m-d H:i:s');
            $info['time'] = time();
            $edit_wash_activity_store = $db_wash_activity_store->where("id='$id'")->setField($info);
            if($edit_wash_activity_store){
                $this->redirect('Activity/store');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '门店管理-门店信息-编辑';//页面title信息
        $this->assign('title', '门店管理-门店信息-编辑');//页面标题
        $this->display();
    }

    /**
     * 门店管理-门店信息-删除
     */
    public function store_del(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        $id = I('get.id');
        $store = $db_wash_activity_store->where("id='$id'")->find();

        /*删除文件*/
        $banner_img_url_path = '..'.$store['banner_img_url'];
        if(file_exists($banner_img_url_path)){
            unlink($banner_img_url_path);
        }

        $del_wash_activity_store = $db_wash_activity_store->where("id='$id'")->delete();
        if($del_wash_activity_store){
            $this->success('删除成功!');
        }else{
            $this->error('删除失败!');
        }
    }

    /**
     * 门店管理-门店信息-详情
     */
    public function store_detail(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        $id = I('get.id');
        $store = $db_wash_activity_store->where("id='$id'")->find();
        $this->assign('store', $store);

        $this->meta_title = '门店管理-门店信息-详情';//页面title信息
        $this->assign('title', '门店管理-门店信息-详情');//页面标题
        $this->display();
    }

    /**
     * 门店管理-门店信息-标题
     */
    public function store_title(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表
        $db_wash_activity_title = M('wash_activity_title');//实例化题目管理表

        $id = I('get.id');
        $store = $db_wash_activity_store->where("id='$id'")->find();
        $this->assign('store', $store);

        $wash_activity_title = $db_wash_activity_title->order('id DESC')->field('id,cid,title,datetime')->select();
        $arr = array();
        foreach ($wash_activity_title as $v){
            $v['status'] = 0;
            if($store['title_id'] != '') {
                if ($v['cid'] == $store['title_id']) {
                    $v['status'] = 1;
                    array_unshift($arr,$v);
                }else{
                    array_push($arr,$v);
                }
            }else{
                array_push($arr,$v);
            }
        }

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 15);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '门店管理-门店信息-标题';//页面title信息
        $this->assign('title', '门店管理-门店信息-标题');//页面标题
        $this->display();
    }

    /**
     * 门店管理-门店信息-标题-启用
     */
    public function store_title_on(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        $store_id = I('get.store_id');
        $cid = I('get.cid');

        $info['title_id'] = $cid;
        $edit_wash_activity_title = $db_wash_activity_store->where("id='$store_id'")->setField($info);
        if($edit_wash_activity_title){
            $this->success('启用成功!',U('Activity/store_title', array('id'=>$store_id)));
        }else{
            $this->error('启用失败!');
        }
    }

    /**
     * 门店管理-门店信息-标题-停用
     */
    public function store_title_off(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        $store_id = I('get.store_id');

        $info['title_id'] = '';
        $edit_wash_activity_title = $db_wash_activity_store->where("id='$store_id'")->setField($info);
        if($edit_wash_activity_title){
            $this->success('停用成功!',U('Activity/store_title', array('id'=>$store_id)));
        }else{
            $this->error('停用失败!');
        }
    }

    /**
     * 门店管理-门店信息-内容
     */
    public function store_content(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表
        $db_wash_activity_content = M('wash_activity_content');//实例化内容管理表

        $id = I('get.id');
        $store = $db_wash_activity_store->where("id='$id'")->find();
        $this->assign('store', $store);

        $c_list = json_decode($store['c_list']);

        $wash_activity_content = $db_wash_activity_content->order('id DESC')->field('id,cid,title,activity_img_url,datetime')->select();
        $arr = array();

        //增加status 默认所有活动停用
        foreach ($wash_activity_content as $k=>$v){
            $wash_activity_content[$k]['status'] = 0;
        }
        //修改添加了该活动的status状态
        if(!empty($c_list)) {
            foreach ($wash_activity_content as $k=>$v){
                foreach ($c_list as $c){
                    if ($v['cid'] == $c) {
                        $wash_activity_content[$k]['status'] = 1;
                    }
                }
            }
        }
        //活动内容列表排序
        foreach ($wash_activity_content as $v){
            if($v['status'] == 1){
                array_unshift($arr,$v);
            }else{
                array_push($arr,$v);
            }
        }

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 15);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '门店管理-门店信息-内容';//页面title信息
        $this->assign('title', '门店管理-门店信息-内容');//页面标题
        $this->display();
    }

    /**
     * 门店管理-门店信息-内容-启用
     */
    public function store_content_on(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        $store_id = I('get.store_id');
        $cid = I('get.cid');
        $store = $db_wash_activity_store->where("id='$store_id'")->find();

        $c_list = json_decode($store['c_list']);

        if(!in_array($cid,$c_list)){
            array_push($c_list, $cid);
            $info['c_list'] = json_encode($c_list, true);
            $edit_wash_activity_content = $db_wash_activity_store->where("id='$store_id'")->setField($info);
            if($edit_wash_activity_content){
                $this->success('启用成功!',U('Activity/store_content', array('id'=>$store_id)));
            }else{
                $this->error('启用失败!');
            }
        }
    }

    /**
     * 门店管理-门店信息-内容-停用
     */
    public function store_content_off(){
        $db_wash_activity_store = M('wash_activity_store');//实例化门店信息表

        $store_id = I('get.store_id');
        $cid = I('get.cid');
        $store = $db_wash_activity_store->where("id='$store_id'")->find();

        $c_list = json_decode($store['c_list']);

        if(in_array($cid,$c_list)){
            $c_key = array_search($cid,$c_list);//搜索某个键值，并返回对应的键名
            unset($c_list[$c_key]);//删除数组元素
            sort($c_list);//数组键名重新排序

            $info['c_list'] = json_encode($c_list, true);
            $edit_wash_activity_content = $db_wash_activity_store->where("id='$store_id'")->setField($info);
            if($edit_wash_activity_content){
                $this->success('停用成功!',U('Activity/store_content', array('id'=>$store_id)));
            }else{
                $this->error('停用失败!');
            }
        }
    }

    /**
     * 活动管理-标题管理
     */
    public function title(){
        $db_wash_activity_title = M('wash_activity_title');//实例化题目管理表

        $wash_activity_title = $db_wash_activity_title->order('id DESC')->field('id,title,datetime')->select();
        $arr = $wash_activity_title;

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 15);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '活动管理-题目管理';//页面title信息
        $this->assign('title', '活动管理-题目管理');//页面标题
        $this->display();
    }

    /**
     * 活动管理-标题管理-新增
     */
    public function title_add(){
        $db_wash_activity_title = M('wash_activity_title');//实例化题目管理表

        if(IS_POST){
            $title = I('post.title');

            $info['title'] = $title;
            /*生成cid*/
            $cid = create_cid('wash_activity_title');
            $info['cid'] = $cid;

            $info['datetime'] = date('Y-m-d H:i:s');
            $info['time'] = time();

            $add_wash_activity_title = $db_wash_activity_title->data($info)->add();
            if($add_wash_activity_title){
                $this->redirect('Activity/title');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '活动管理-题目管理-新增';//页面title信息
        $this->assign('title', '活动管理-题目管理-新增');//页面标题
        $this->display();
    }

    /**
     * 活动管理-标题管理-编辑
     */
    public function title_edit(){
        $db_wash_activity_title = M('wash_activity_title');//实例化题目管理表

        $id = I('get.id');
        $title = $db_wash_activity_title->where("id='$id'")->find();
        $this->assign('titles', $title);

        if(IS_POST){
            $title = I('post.title');

            $info['title'] = $title;

            /*更新时间*/
            $info['datetime'] = date('Y-m-d H:i:s');
            $info['time'] = time();

            $edit_wash_activity_title = $db_wash_activity_title->where("id='$id'")->setField($info);
            if($edit_wash_activity_title){
                $this->redirect('Activity/title');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '活动管理-题目管理-编辑';//页面title信息
        $this->assign('title', '活动管理-题目管理-编辑');//页面标题
        $this->display();
    }

    /**
     * 活动管理-标题管理-删除
     */
    public function title_del(){
        $db_wash_activity_title = M('wash_activity_title');//实例化题目管理表

        $id = I('get.id');

        $del_wash_activity_title = $db_wash_activity_title->where("id='$id'")->delete();
        if($del_wash_activity_title){
            $this->success('删除成功!');
        }else{
            $this->error('删除失败!');
        }
    }

    /**
     * 活动管理-标题管理-详情
     */
    public function title_detail(){
        $db_wash_activity_title = M('wash_activity_title');//实例化题目管理表

        $id = I('get.id');
        $title = $db_wash_activity_title->where("id='$id'")->find();
        $this->assign('titles', $title);

        $this->meta_title = '活动管理-题目管理-详情';//页面title信息
        $this->assign('title', '活动管理-题目管理-详情');//页面标题
        $this->display();
    }

    /**
     * 活动管理-内容管理
     */
    public function content(){
        $db_wash_activity_content = M('wash_activity_content');//实例化内容管理表

        $wash_activity_content = $db_wash_activity_content->order('id DESC')->field('id,activity_img_url,title,datetime')->select();
        $arr = $wash_activity_content;

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 10);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '活动管理-内容管理';//页面title信息
        $this->assign('title', '活动管理-内容管理');//页面标题
        $this->display();
    }

    /**
     * 活动管理-内容管理-新增
     */
    public function content_add(){
        $db_wash_activity_content = M('wash_activity_content');//实例化内容管理表

        if(IS_POST){
            $title = I('post.title');

            /*文件上传*/
            $savename = time().'_'.mt_rand(1000000,9999999);
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 2048000;// 设置附件上传大小 2M=2048000
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
            $upload->savePath = './Activity/content/picture/'; // 设置附件上传（子）目录
            $upload->saveName = $savename; // 上传文件的文件名
            //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
            $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
            $upload->replace = true; // true会存在同名文件将覆盖
            // 上传文件
            $upload_info = $upload->upload();
            if (!$upload_info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else {
                $activity_img_url = "/wash_helper/Uploads/Activity/content/picture/".$upload_info['file']['savename'];
                $info['activity_img_url'] = $activity_img_url;
                $info['title'] = $title;

                /*生成cid*/
                $cid = create_cid('wash_activity_content');
                $info['cid'] = $cid;

                $info['datetime'] = date('Y-m-d H:i:s');
                $info['time'] = time();

                $add_wash_activity_content = $db_wash_activity_content->data($info)->add();
                if ($add_wash_activity_content) {
                    $this->redirect('Activity/content');
                } else {
                    $this->error('保存失败!');
                }
            }
        }

        $this->meta_title = '活动管理-内容管理-新增';//页面title信息
        $this->assign('title', '活动管理-内容管理-新增');//页面标题
        $this->display();
    }

    /**
     * 活动管理-内容管理-编辑
     */
    public function content_edit(){
        $db_wash_activity_content = M('wash_activity_content');//实例化内容管理表

        $id = I('get.id');
        $content = $db_wash_activity_content->where("id='$id'")->find();
        $this->assign('content', $content);

        if(IS_POST){
            $title = I('post.title');

            //判断是否有文件上传
            if($_FILES["file"]["tmp_name"]){
                /*文件上传*/
                $savename = time().'_'.mt_rand(1000000,9999999);
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 2048000;// 设置附件上传大小 2M=2048000
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
                $upload->savePath = './Activity/content/picture/'; // 设置附件上传（子）目录
                $upload->saveName = $savename; // 上传文件的文件名
                //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
                $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
                $upload->replace = true; // true会存在同名文件将覆盖
                // 上传文件
                $upload_info = $upload->upload();
                if (!$upload_info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $activity_img_url = "/wash_helper/Uploads/Activity/content/picture/".$upload_info['file']['savename'];
                    $info['activity_img_url'] = $activity_img_url;

                    /*删除文件*/
                    $activity_img_url_path = '..'.$content['activity_img_url'];
                    if(file_exists($activity_img_url_path)){
                        unlink($activity_img_url_path);
                    }
                }
            }

            $info['title'] = $title;

            /*更新时间*/
            $info['datetime'] = date('Y-m-d H:i:s');
            $info['time'] = time();

            $edit_wash_activity_content = $db_wash_activity_content->where("id='$id'")->setField($info);
            if($edit_wash_activity_content){
                $this->redirect('Activity/content');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '活动管理-内容管理-编辑';//页面title信息
        $this->assign('title', '活动管理-内容管理-编辑');//页面标题
        $this->display();
    }

    /**
     * 活动管理-内容管理-删除
     */
    public function content_del(){
        $db_wash_activity_content = M('wash_activity_content');//实例化内容管理表

        $id = I('get.id');
        $content = $db_wash_activity_content->where("id='$id'")->find();

        /*删除文件*/
        $activity_img_url_path = '..'.$content['activity_img_url'];
        if(file_exists($activity_img_url_path)){
            unlink($activity_img_url_path);
        }

        $del_wash_activity_content = $db_wash_activity_content->where("id='$id'")->delete();
        if($del_wash_activity_content){
            $this->success('删除成功!');
        }else{
            $this->error('删除失败!');
        }
    }

    /**
     * 活动管理-内容管理-详情
     */
    public function content_detail(){
        $db_wash_activity_content = M('wash_activity_content');//实例化内容管理表

        $id = I('get.id');
        $content = $db_wash_activity_content->where("id='$id'")->find();
        $this->assign('content', $content);

        $this->meta_title = '活动管理-内容管理-详情';//页面title信息
        $this->assign('title', '活动管理-内容管理-详情');//页面标题
        $this->display();
    }

    /**
     * 获取地级市
     */
    public function get_citys(){
        $db_wash_region = M('wash_region');//实例化地址选择表

        $where['top_parentid'] = I('province_id');
        $where['level'] = 2;
        $list = $db_wash_region->where($where)->select();
        $data=array('status'=>0,'city'=>$list);
        $this->ajaxReturn($data);
    }

    /**
     * 获取区/县
     */
    public function get_district(){
        $db_wash_region = M('wash_region');//实例化地址选择表

        $where['parent_id'] = I('city_id');
        $where['level'] = 3;
        $list = $db_wash_region->where($where)->select();
        $data=array('status'=>0,'district'=>$list);
        $this->ajaxReturn($data);
    }

    /**
     * 删除图文混排图片
     * str  $str    要提去的字符串
     * int  $type   1提取图片 2提取src和href
     */
    public function del_images_path($str,$type=1){
        if($type == 1){
            preg_match('/<img.+src=\"?(.+\.(jpg|gif|jpeg|png))\"?.+>/i',$str,$match);
            if($match[1]){
                foreach($match[1] as $v){
                    $images_path = '..'.$v;
                    if(file_exists($images_path)){
                        unlink($images_path);
                    }
                }
            }
        }
        if($type == 2){
            preg_match_all('/(src|href)=\"?(.+\.(jpg|gif|jpeg|png))\"/i',$str,$match);
            if($match[2]){
                foreach($match[2] as $v){
                    $images_path = '..'.$v;
                    if(file_exists($images_path)){
                        unlink($images_path);
                    }
                }
            }
        }
    }

    /**
     * SFA项目对接curl的POST请求
     * @param String $url     请求地址
     * @param Array  $data    POST数据
     *
     * @return String
     */
    public function sfa_post_curl($url, $data){
        $headers = array(
            "Content-type: application/json;charset='utf-8'",
            "Accept: application/json",
            "Cache-Control: no-cache",
            "Pragma: no-cache"
        );
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置超时
        if(0 === strpos(strtolower($url), 'https')) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //对认证证书来源的检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //从证书中检查SSL加密算法是否存在
        }
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, true));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $res = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec返回TRUE 设置  curl_exec返回json(此处) 失败都返回FALSE
        curl_close($ch);

        return $res;
    }
}
